﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace XDSoft.DAL
{
	/// <summary>
	/// 数据访问类:TB_Brand
	/// </summary>
	public partial class TB_Brand
	{
		public TB_Brand()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("id", "TB_Brand"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from TB_Brand");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(XDSoft.Model.TB_Brand model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into TB_Brand(");
            strSql.Append("icon,regCode,typeCode,applyDate,applyManName,applyManAddress,applyManEnName,applyManEnAddress,specialStartDate,specialEndDate,brandType,clientService,productList,agentFee,netApplyDate,docSubmitDate,assignDate,status,memo,client,serviceExpire)");
			strSql.Append(" values (");
            strSql.Append("@icon,@regCode,@typeCode,@applyDate,@applyManName,@applyManAddress,@applyManEnName,@applyManEnAddress,@specialStartDate,@specialEndDate,@brandType,@clientService,@productList,@agentFee,@netApplyDate,@docSubmitDate,@assignDate,@status,@memo,@client,@serviceExpire)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@icon", SqlDbType.VarChar,100),
					new SqlParameter("@regCode", SqlDbType.VarChar,50),
					new SqlParameter("@typeCode", SqlDbType.VarChar,10),
					new SqlParameter("@applyDate", SqlDbType.DateTime),
					new SqlParameter("@applyManName", SqlDbType.VarChar,50),
					new SqlParameter("@applyManAddress", SqlDbType.VarChar,100),
					new SqlParameter("@applyManEnName", SqlDbType.VarChar,50),
					new SqlParameter("@applyManEnAddress", SqlDbType.VarChar,100),
					new SqlParameter("@specialStartDate", SqlDbType.DateTime),
					new SqlParameter("@specialEndDate", SqlDbType.DateTime),
					new SqlParameter("@brandType", SqlDbType.VarChar,20),
					new SqlParameter("@clientService", SqlDbType.VarChar,20),
					new SqlParameter("@productList", SqlDbType.VarChar,200),
					new SqlParameter("@agentFee", SqlDbType.Decimal,9),
					new SqlParameter("@netApplyDate", SqlDbType.DateTime),
					new SqlParameter("@docSubmitDate", SqlDbType.DateTime),
					new SqlParameter("@assignDate", SqlDbType.DateTime),
					new SqlParameter("@status", SqlDbType.VarChar,20),
					new SqlParameter("@memo", SqlDbType.VarChar,200),
					new SqlParameter("@client", SqlDbType.Int,4),
                    new SqlParameter("@serviceExpire", SqlDbType.DateTime)};
			parameters[0].Value = model.icon;
			parameters[1].Value = model.regCode;
			parameters[2].Value = model.typeCode;
			parameters[3].Value = model.applyDate;
			parameters[4].Value = model.applyManName;
			parameters[5].Value = model.applyManAddress;
			parameters[6].Value = model.applyManEnName;
			parameters[7].Value = model.applyManEnAddress;
			parameters[8].Value = model.specialStartDate;
			parameters[9].Value = model.specialEndDate;
			parameters[10].Value = model.brandType;
			parameters[11].Value = model.clientService;
			parameters[12].Value = model.productList;
			parameters[13].Value = model.agentFee;
			parameters[14].Value = model.netApplyDate;
			parameters[15].Value = model.docSubmitDate;
			parameters[16].Value = model.assignDate;
			parameters[17].Value = model.status;
			parameters[18].Value = model.memo;
			parameters[19].Value = model.client;
            parameters[20].Value = model.serviceExpire;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(XDSoft.Model.TB_Brand model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update TB_Brand set ");
			strSql.Append("icon=@icon,");
			strSql.Append("regCode=@regCode,");
			strSql.Append("typeCode=@typeCode,");
			strSql.Append("applyDate=@applyDate,");
			strSql.Append("applyManName=@applyManName,");
			strSql.Append("applyManAddress=@applyManAddress,");
			strSql.Append("applyManEnName=@applyManEnName,");
			strSql.Append("applyManEnAddress=@applyManEnAddress,");
			strSql.Append("specialStartDate=@specialStartDate,");
			strSql.Append("specialEndDate=@specialEndDate,");
			strSql.Append("brandType=@brandType,");
			strSql.Append("clientService=@clientService,");
			strSql.Append("productList=@productList,");
			strSql.Append("agentFee=@agentFee,");
			strSql.Append("netApplyDate=@netApplyDate,");
			strSql.Append("docSubmitDate=@docSubmitDate,");
			strSql.Append("assignDate=@assignDate,");
			strSql.Append("status=@status,");
			strSql.Append("memo=@memo,");
			strSql.Append("client=@client,");
            strSql.Append("serviceExpire=@serviceExpire");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@icon", SqlDbType.VarChar,100),
					new SqlParameter("@regCode", SqlDbType.VarChar,50),
					new SqlParameter("@typeCode", SqlDbType.VarChar,10),
					new SqlParameter("@applyDate", SqlDbType.DateTime),
					new SqlParameter("@applyManName", SqlDbType.VarChar,50),
					new SqlParameter("@applyManAddress", SqlDbType.VarChar,100),
					new SqlParameter("@applyManEnName", SqlDbType.VarChar,50),
					new SqlParameter("@applyManEnAddress", SqlDbType.VarChar,100),
					new SqlParameter("@specialStartDate", SqlDbType.DateTime),
					new SqlParameter("@specialEndDate", SqlDbType.DateTime),
					new SqlParameter("@brandType", SqlDbType.VarChar,20),
					new SqlParameter("@clientService", SqlDbType.VarChar,20),
					new SqlParameter("@productList", SqlDbType.VarChar,200),
					new SqlParameter("@agentFee", SqlDbType.Decimal,9),
					new SqlParameter("@netApplyDate", SqlDbType.DateTime),
					new SqlParameter("@docSubmitDate", SqlDbType.DateTime),
					new SqlParameter("@assignDate", SqlDbType.DateTime),
					new SqlParameter("@status", SqlDbType.VarChar,20),
					new SqlParameter("@memo", SqlDbType.VarChar,200),
					new SqlParameter("@client", SqlDbType.Int,4),
                    new SqlParameter("@serviceExpire", SqlDbType.DateTime),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.icon;
			parameters[1].Value = model.regCode;
			parameters[2].Value = model.typeCode;
			parameters[3].Value = model.applyDate;
			parameters[4].Value = model.applyManName;
			parameters[5].Value = model.applyManAddress;
			parameters[6].Value = model.applyManEnName;
			parameters[7].Value = model.applyManEnAddress;
			parameters[8].Value = model.specialStartDate;
			parameters[9].Value = model.specialEndDate;
			parameters[10].Value = model.brandType;
			parameters[11].Value = model.clientService;
			parameters[12].Value = model.productList;
			parameters[13].Value = model.agentFee;
			parameters[14].Value = model.netApplyDate;
			parameters[15].Value = model.docSubmitDate;
			parameters[16].Value = model.assignDate;
			parameters[17].Value = model.status;
			parameters[18].Value = model.memo;
			parameters[19].Value = model.client;
            parameters[20].Value = model.serviceExpire;
			parameters[21].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TB_Brand ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TB_Brand ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XDSoft.Model.TB_Brand GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 id,icon,regCode,typeCode,applyDate,applyManName,applyManAddress,applyManEnName,applyManEnAddress,specialStartDate,specialEndDate,brandType,clientService,productList,agentFee,netApplyDate,docSubmitDate,assignDate,status,memo,client,serviceExpire from TB_Brand ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			XDSoft.Model.TB_Brand model=new XDSoft.Model.TB_Brand();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["icon"]!=null && ds.Tables[0].Rows[0]["icon"].ToString()!="")
				{
					model.icon=ds.Tables[0].Rows[0]["icon"].ToString();
				}
				if(ds.Tables[0].Rows[0]["regCode"]!=null && ds.Tables[0].Rows[0]["regCode"].ToString()!="")
				{
					model.regCode=ds.Tables[0].Rows[0]["regCode"].ToString();
				}
				if(ds.Tables[0].Rows[0]["typeCode"]!=null && ds.Tables[0].Rows[0]["typeCode"].ToString()!="")
				{
					model.typeCode=ds.Tables[0].Rows[0]["typeCode"].ToString();
				}
				if(ds.Tables[0].Rows[0]["applyDate"]!=null && ds.Tables[0].Rows[0]["applyDate"].ToString()!="")
				{
					model.applyDate=DateTime.Parse(ds.Tables[0].Rows[0]["applyDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["applyManName"]!=null && ds.Tables[0].Rows[0]["applyManName"].ToString()!="")
				{
					model.applyManName=ds.Tables[0].Rows[0]["applyManName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["applyManAddress"]!=null && ds.Tables[0].Rows[0]["applyManAddress"].ToString()!="")
				{
					model.applyManAddress=ds.Tables[0].Rows[0]["applyManAddress"].ToString();
				}
				if(ds.Tables[0].Rows[0]["applyManEnName"]!=null && ds.Tables[0].Rows[0]["applyManEnName"].ToString()!="")
				{
					model.applyManEnName=ds.Tables[0].Rows[0]["applyManEnName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["applyManEnAddress"]!=null && ds.Tables[0].Rows[0]["applyManEnAddress"].ToString()!="")
				{
					model.applyManEnAddress=ds.Tables[0].Rows[0]["applyManEnAddress"].ToString();
				}
				if(ds.Tables[0].Rows[0]["specialStartDate"]!=null && ds.Tables[0].Rows[0]["specialStartDate"].ToString()!="")
				{
					model.specialStartDate=DateTime.Parse(ds.Tables[0].Rows[0]["specialStartDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["specialEndDate"]!=null && ds.Tables[0].Rows[0]["specialEndDate"].ToString()!="")
				{
					model.specialEndDate=DateTime.Parse(ds.Tables[0].Rows[0]["specialEndDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["brandType"]!=null && ds.Tables[0].Rows[0]["brandType"].ToString()!="")
				{
					model.brandType=ds.Tables[0].Rows[0]["brandType"].ToString();
				}
				if(ds.Tables[0].Rows[0]["clientService"]!=null && ds.Tables[0].Rows[0]["clientService"].ToString()!="")
				{
					model.clientService=ds.Tables[0].Rows[0]["clientService"].ToString();
				}
				if(ds.Tables[0].Rows[0]["productList"]!=null && ds.Tables[0].Rows[0]["productList"].ToString()!="")
				{
					model.productList=ds.Tables[0].Rows[0]["productList"].ToString();
				}
				if(ds.Tables[0].Rows[0]["agentFee"]!=null && ds.Tables[0].Rows[0]["agentFee"].ToString()!="")
				{
					model.agentFee=decimal.Parse(ds.Tables[0].Rows[0]["agentFee"].ToString());
				}
				if(ds.Tables[0].Rows[0]["netApplyDate"]!=null && ds.Tables[0].Rows[0]["netApplyDate"].ToString()!="")
				{
					model.netApplyDate=DateTime.Parse(ds.Tables[0].Rows[0]["netApplyDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["docSubmitDate"]!=null && ds.Tables[0].Rows[0]["docSubmitDate"].ToString()!="")
				{
					model.docSubmitDate=DateTime.Parse(ds.Tables[0].Rows[0]["docSubmitDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["assignDate"]!=null && ds.Tables[0].Rows[0]["assignDate"].ToString()!="")
				{
					model.assignDate=DateTime.Parse(ds.Tables[0].Rows[0]["assignDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["status"]!=null && ds.Tables[0].Rows[0]["status"].ToString()!="")
				{
					model.status=ds.Tables[0].Rows[0]["status"].ToString();
				}
				if(ds.Tables[0].Rows[0]["memo"]!=null && ds.Tables[0].Rows[0]["memo"].ToString()!="")
				{
					model.memo=ds.Tables[0].Rows[0]["memo"].ToString();
				}
				if(ds.Tables[0].Rows[0]["client"]!=null && ds.Tables[0].Rows[0]["client"].ToString()!="")
				{
					model.client=int.Parse(ds.Tables[0].Rows[0]["client"].ToString());
				}
                if (ds.Tables[0].Rows[0]["serviceExpire"] != null && ds.Tables[0].Rows[0]["serviceExpire"].ToString() != "")
                {
                    model.serviceExpire = DateTime.Parse(ds.Tables[0].Rows[0]["serviceExpire"].ToString());
                }
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select id,icon,regCode,typeCode,applyDate,applyManName,applyManAddress,applyManEnName,applyManEnAddress,specialStartDate,specialEndDate,brandType,clientService,productList,agentFee,netApplyDate,docSubmitDate,assignDate,status,memo,client,serviceExpire ");
			strSql.Append(" FROM TB_Brand ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" id,icon,regCode,typeCode,applyDate,applyManName,applyManAddress,applyManEnName,applyManEnAddress,specialStartDate,specialEndDate,brandType,clientService,productList,agentFee,netApplyDate,docSubmitDate,assignDate,status,memo,client,serviceExpire ");
			strSql.Append(" FROM TB_Brand ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM TB_Brand ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from TB_Brand T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "TB_Brand";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

